package com.nfha.algorithm.geo.crossline;

import java.awt.Point;

public class GeoLine {

	
	
	public final double start_lon,start_lat,end_lon,end_lat;
	
	public final Point start_point,end_point;
	
	public final double bear;
	
	public GeoLine(GeoPoint start,GeoPoint end) {
		
		super();
		this.start_lon = start.lon;
		this.start_lat = start.lat;
		this.end_lon = end.lon;
		this.end_lat = end.lat;
		
		start_point=new Point();
		
		
		start_point.setLocation(start.x,start.y);
		
		end_point=new Point();
		
		end_point.setLocation(end.x,end.y);
		
		bear=bearing(start_lat, start_lon,end_lat, end_lon);
	}
	

	public GeoLine(double start_lon, double start_lat, double end_lon, double end_lat) {
		super();
		this.start_lon = start_lon;
		this.start_lat = start_lat;
		this.end_lon = end_lon;
		this.end_lat = end_lat;
		
		start_point=new Point();
		
		double[] xy=GeoPoint.millierConvertion(start_lon, start_lat);
		
		start_point.setLocation(xy[0], xy[1]);
		
		end_point=new Point();
		xy=GeoPoint.millierConvertion(end_lon, end_lat);
		end_point.setLocation(xy[0], xy[1]);
		bear=bearing(start_lat, start_lon,end_lat, end_lon);
		
	}

	
	
	
	@Override
	public String toString() {
		return "GeoLine [start_lon=" + start_lon + ", start_lat=" + start_lat + ", end_lon=" + end_lon + ", end_lat="
				+ end_lat + ", start_point=" + start_point + ", end_point=" + end_point + ", bear=" + bear + "]";
	}


	public static double bearing(double lat1, double lon1, double lat2, double lon2){
        double longitude1 = lon1;
        double longitude2 = lon2;
        double latitude1 = Math.toRadians(lat1);
        double latitude2 = Math.toRadians(lat2);
        double longDiff = Math.toRadians(longitude2-longitude1);
        double y = Math.sin(longDiff)*Math.cos(latitude2);
        double x = Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);
        return (Math.toDegrees(Math.atan2(y, x))+360)%360;
    }
	 
	
	
	
	
}
